<template>
    <div class="top">
        <!-- 顶部版心  -->
        <div class="container">
            <!-- 顶部背景 -->
            <div class="icon">
                <img src="https://course.myhope365.com/img/logo-text.321d0dc2.png" alt="">
            </div>
            <!-- 头部 -->
            <header>
                <div>
                    <div class="nav">
                        <!-- 导航栏 -->
                        <div class="header-nav">
                            <div class="nav-container">
                                <ul class="dropdown">
                                    <li><router-link to="/" class="data" style="color: #fff;">首页</router-link></li>
                                    <li>
                                        <div class="data" @click="toggleUl">课程分类
                                            <i class="action">
                                                <img src="../assets/向下.png" alt="">
                                            </i>
                                            <div id="cSelect">
                                                <ul v-if="isUlVisible">
                                                    <li v-for="item in cList" :key="item.subjectId">
                                                        <div @click="goToSearch(item.subjectId)">{{ item.subjectTitle }}</div>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </li>
                                    <li><div class="data">教学管理平台</div></li>
                                </ul>
                            </div>
                        </div>
                        <!-- 搜索框 -->
                        <span class="search">
                            <span>
                                <input v-model="keyword" type="text" placeholder="搜索课程~">
                                <span @click="goSearch">
                                    <button @click="handleSearch">
                                        <img src="" alt="">
                                    </button>
                                </span> 
                            </span>
                        </span>
                        <!-- 登录注册 -->
                        <div class="login">
                            <div class="login-box">
                                <!-- 购物车 -->
                                <router-link to="/shop" class="white-box">
                                    <span>
                                        <img src="" alt="">
                                    </span>
                                </router-link>
                            </div>
                            <div class="user">
                                <div class="open" v-if="$store.state.login">开通VIP免费观看全站视频</div>
                                <loginView>登录/注册</loginView>
                            </div>
                        </div>
                    </div>
                </div>
            </header>
        </div>
    </div>
</template>
<script>
    import { getDetail } from "../api/request"
    import loginView from "../views/login/loginView.vue"
    export default {
        data() {
            return {
                cList:[],
                isUlVisible: false,
                keyword: '',
            };
        },
        components: {
            loginView,
        },
        methods: {
            toggleUl() {
                this.isUlVisible = !this.isUlVisible;
            },
            goSearch() {
                this.$router.push({
                    path: '/search',
                    query: {
                        keyword: this.keyword
                    }
                }).catch(err => {
                    // 忽略导航重复错误
                    if (err.name !== 'NavigationDuplicated') {
                        throw err;
                    }
                });
            },
            handleSearch() {
                // 触发自定义事件，传递 keyword
                this.$emit('search', this.keyword);
            },
            // 新增跳转方法，传递 subjectId
            goToSearch(subjectId) {
                this.$router.push({
                    path: '/search',
                    query: {
                        subjectId: subjectId
                    }
                }).catch(err => {
                    if (err.name !== 'NavigationDuplicated') {
                        throw err;
                    }
                });
            }
        },
        beforeCreate() {
        },
        created() {
            getDetail().then(res => {
                this.cList = res.rows;
            })
        },
        beforeMount() {
          
        },
        mounted() {},
        beforeUpdate() {},
        updated() {},
        beforeDestroy() {},
        destroyed() {},
    };
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
ol,
ul {
  list-style: none;
}
a {
  text-decoration: none!important;
}
a:hover {
  color: #00cf8c;
}
img {
  border: 0;
  vertical-align: middle;
}
.top {
  width: 100%;
  height: 295px;
  background: no-repeat url(https://course.myhope365.com/img/index-top-bg.564836d0.png);
  background-size: 100% 100%;
    .container {
        width: 100%;
        margin: 0 auto;
        .icon {
            width: 1160px;
            height: 168px;
            margin: 0px auto;
            display: flex;
            align-items: center;
            flex-direction: column;
            > img {
                width: 212px;
                margin: 50px 0 50px;
            }
        }
        > header {
            width: 100%;
            height: 80px;
            background: #fff;
            background-color: transparent !important;
            color: #fff;
            > div {
                margin: 0 auto;
                width: 1160px;
            }
        }
        .nav {
            height: 80px;
            display: flex;
            align-items: center;
            .header-nav {
                margin-right: 30px;
                margin-left: 40px;
                .nav-container {
                    color: #fff;
                    > ul > li {
                        cursor: pointer;
                        float: left;
                        margin: 0 25px 0 0;
                        font-size: 16px;
                        > div,a {
                            color: #fff;
                            font-size: 18px;
                            font-weight: 600;
                        }
                    }
                }

            }
        }
    }
}
#cSelect ul{
    position: absolute;
    background-color: white;
    color: black;
    padding: 10px; 
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); 
    z-index: 1000; 
    >li {
        float: none;
        margin: 5px 0;
        >div {
            color: black; 
            font-size: 14px; 
            font-weight: normal; 
        }
    }
}
.nav > .search {
  width: 300px;
  display: inline-block;
  text-align: start;
  vertical-align: top;
  margin-left: auto;
}
.search > span {
  box-sizing: border-box;
  color: rgba(0, 0, 0, 0.65);
  font-size: 14px;
  line-height: 1.5;
  list-style: none;
  position: relative;
  display: table;
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}
.search > span > input {
  height: 38px;
  border-radius: 20px;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  float: left;
  width: 85%;
  margin-bottom: 0;
  padding: 6px 11px;
  font-size: 16px;
  box-sizing: border-box;
  margin: 0;
  line-height: 1.5;
  background-color: #fff;
  background-image: none;
  border: 1px solid #d9d9d9;
}
.search > span > span {
  position: relative;
  display: inline-block;
  box-sizing: border-box;
  height: 38px;
  padding: 0 11px;
  color: rgba(0, 0, 0, 0.65);
  border: 1px solid #d9d9d9;
  border-radius: 20px;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.search > span > span > button {
  height: 33px;
  margin-left: -2px;
  position: relative;
  display: inline-block;
  background-color: transparent;
  text-align: center;
  border-color: #d9d9d9;
  border: 1px solid transparent;
}
.nav > .login {
  height: 100%;
  font-size: 14px;
  display: flex;
  align-items: center;
}
.login > .login-box .white-box {
  width: 36px;
  height: 36px;
  background: #fff;
  border-radius: 50%;
  margin: 0 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.white-box > span {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  color: rgba(0, 0, 0, 0.65);
  font-size: 14px;
  line-height: 1;
  list-style: none;
  position: relative;
}
.white-box > span > img {
  vertical-align: middle;
  width: 20px;
}
.login > .user {
  display: flex;
  align-items: center;
  position: relative;
  justify-content: center;
}
.user .open {
  position: absolute;
  font-size: 10px;
  top: -50px;
  width: 155px;
  height: 41px;
  text-align: center;
  line-height: 30px;
  font-weight: 500;
  background: url();
  background-size: 100%;
}
.user .register1 {
  width: 94px;
  height: 30px;
  background: #fff;
  color: #00cf8c;
  border-radius: 15px;
  font-weight: 600;
  font-size: 15px;
  text-align: center;
}
</style>